package com.github.service.cms.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.common.constants.ConstantEditableValue;
import com.github.common.constants.ConstantEnabledValue;
import com.github.common.exception.BusinessException;
import com.github.common.exception.ExceptionCode;
import com.github.dao.cms.ResourceDAO;
import com.github.form.cms.ResourceForm;
import com.github.module.cms.Resource;
import com.github.service.cms.ResourceService;

/**
 * <p>User: Mu Yunlong
 * <p>Date: 2016年2月23日 下午2:56:21
 * <p>Version: 1.0
 */
@Service
public class ResourceServiceImpl implements  ResourceService {

    @Autowired
    ResourceDAO resourceDAO;

    /* (non-Javadoc)
     * @see com.github.service.cms.impl.ResourceService#findAll()
     */
    @Override
    public List<Resource> findAll() {
        return resourceDAO.find();
    }

    /* (non-Javadoc)
     * @see com.github.service.cms.impl.ResourceService#get(java.lang.String)
     */
    @Override
    public Resource get(String id) {
        return resourceDAO.get(id);
    }

    /* (non-Javadoc)
     * @see com.github.service.cms.impl.ResourceService#update(com.github.form.cms.ResourceForm)
     */
    @Override
    public int update(ResourceForm form) {
       return resourceDAO.update(form);
    }

    /* (non-Javadoc)
     * @see com.github.service.cms.impl.ResourceService#add(com.github.form.cms.ResourceForm)
     */
    @Override
    public int add(ResourceForm form) {
        return resourceDAO.add(form);
    }

    /* (non-Javadoc)
     * @see com.github.service.cms.impl.ResourceService#delete(java.lang.String)
     */
    @Override
    public int delete(String id) {
        return resourceDAO.delete(id);
        
    }

    /* (non-Javadoc)
     * @see com.github.service.cms.impl.ResourceService#enable(java.lang.String)
     */
    public void enable(String id) throws BusinessException {
        if (resourceDAO.checkEditable(id) != ConstantEditableValue.EDITABLE_INT) {
            throw new BusinessException(ExceptionCode.CODE_1000);
        } else {
            resourceDAO.changeEnabled(id, ConstantEnabledValue.ENABLED_INT);
        }
        
    }

    /* (non-Javadoc)
     * @see com.github.service.cms.impl.ResourceService#disable(java.lang.String)
     */
    @Override
    public void disable(String id) throws BusinessException {
        if (resourceDAO.checkEditable(id) != ConstantEditableValue.EDITABLE_INT) {
            throw new BusinessException(ExceptionCode.CODE_1000);
        } else {
            resourceDAO.changeEnabled(id, ConstantEnabledValue.DISABLED_INT);
        }
    }


}
